home *** CD-ROM | disk | FTP | other *** search
- /*
- * MakeWrite File I/O Routines.
- */
-
- # include "MWFileStuff.h"
- # include "MWMaca.h"
- # include "MakeWrite.h"
-
-
- static SFReply inFile;
- static SFReply outFile;
- static Str255 fName; /* map name and volume reference number */
- static short vRefNum;
-
-
- /*
- * Clear the current file name, and reset the title of the map
- * window. Obviously, the first call to this must occur
- * after the map window is initialized.
- */
-
- void
- ClearMapName (void)
- {
- CopyString ("\pUntitled", fName);
- SetMapName (fName);
- }
-
-
- void
- SetMapName (StringPtr name)
- {
- Str255 s;
-
- CopyString ("\pMap Name: ", s);
- AppendString (name, s);
- SetWTitle (mapWind, s);
- }
-
-
- /*
- * Check a ConvSpec to see that it makes sense
- */
-
- static Boolean
- CheckMSpec (MapSpec *m)
- {
- short errCnt = 0;
-
- if (FontIndex (m->font) < 0)
- {
- ErrWindMsge ("\pUnknown font number: ", m->font);
- ++errCnt;
- }
- if (SizeIndex ((short) m->size) < 0)
- {
- ErrWindMsge ("\pUnknown size: ", (short) m->size);
- ++errCnt;
- }
-
- return (errCnt == 0);
- }
-
-
- /*
- * Read paragraph style variables
- */
-
- static Boolean
- ReadParaStyle (short f, short version)
- {
- if (version == 1)
- {
- InitParaStyle (); /* use defaults */
- return (true);
- }
- return (FileRead (f, (Ptr) ¶Style, (long) sizeof (ParaStyle))
- && ReadString (f, paraMark)
- && ReadString (f, pageMark)
- && ReadString (f, periodStr)
- && ReadString (f, quoteStr));
- }
-
-
- /*
- * Read one map specification from a file. First read the MapSpec
- * structure, being careful not to clobber the mark string handle.
- * Then read the string that gets put into the handle (first the
- * length byte, then the rest of the string).
- */
-
- static Boolean
- ReadMSpec (short f, MapSpec *m)
- {
- StringHandle hStr;
- Boolean ok;
-
- hStr = m->mark; /* save since clobbered by next read */
- ok = FileRead (f, (Ptr) m, (long) sizeof (MapSpec));
- m->mark = hStr;
- if (ok)
- {
- HLock ((Handle) hStr);
- ok = ReadString (f, *hStr);
- HUnlock ((Handle) hStr);
- }
- return (ok);
- }
-
-
- /*
- * Read map specifications from a file. Duplicate or illegal
- * specifications are not added.
- *
- * Assumes file is open and all mappings already cleared (for Open) or
- * not (for Append). If setParaInfo is true (Open), paragraph style
- * info is set. If it's false (Append), it's not set.
- *
- * Does not set undo variables or map-changed variables.
- */
-
- static void
- ReadMap (short f, Boolean setParaInfo)
- {
- MapSpec mSpec;
- short badCnt = 0;
- short ver;
- short i, nLines;
- Str255 str;
- Boolean ok;
-
- InitMSpec (&mSpec);
- ErrWindInit (inFile.fName);
- if (!ReadInteger (f, &ver))
- {
- Message1 ("\pCan't read map file.");
- }
- else if (ver != mapVersion && ver != 1)
- {
- NumToString ((long) ver, str);
- Message2 ("\pUnknown map file version: ", str);
- }
- else if (ReadInteger (f, &nLines))
- {
- ok = true;
- for (i = 0; i < nLines; ++i)
- {
- ok = false;
- if (!ReadMSpec (f, &mSpec))
- break;
- ok = true;
-
- /*
- * Need to check legality here, since the file may have been
- * created using the fonts from a font list other than the current
- * one.
- */
-
- if (CheckMSpec (&mSpec) == false)
- {
- ++badCnt;
- continue; /* its bad - don't use it */
- }
-
- if (StatMSpec (&mSpec))
- {
- ++badCnt;
- continue; /* already exists - don't use dups */
- }
-
- if (InsertMapping (&mSpec, mapList->nLines) == false)
- break;
- }
- if (ok && setParaInfo)
- (void) ReadParaStyle (f, ver);
- }
-
- TermMSpec (&mSpec);
-
- (void) FSClose (f);
- ScrollToLine (mapList, 0); /* force scroll to top */
- SelectMapping (noLine); /* but leave unselected */
-
- if (badCnt)
- {
- NumToString ((long) badCnt, str);
- Message2 (str, "\p illegal or duplicate specifications were found and ignored");
- }
- }
-
-
- /*
- * Open a map. Clobber the current map first, set the map name
- * afterward.
- */
-
- Boolean
- OpenMap (void)
- {
- short f;
-
- if (GetInputFile ("\pOpen", mwType, &inFile)
- && OpenInputFile (&inFile, &f))
- {
- ClobberMap ();
- ReadMap (f, true);
- CopyString (inFile.fName, fName);
- vRefNum = inFile.vRefNum;
- SetMapName (fName);
- return (true);
- }
- return (false);
- }
-
-
- /*
- * Add a map to the current map. Doesn't set map name afterward.
- */
-
- Boolean
- AddMap (void)
- {
- short f;
-
- if (GetInputFile ("\pAdd", mwType, &inFile)
- && OpenInputFile (&inFile, &f))
- {
- ReadMap (f, false);
- return (true);
- }
- return (false);
- }
-
-
- /*
- * Save map to file. askForName is true if should ask for name,
- * otherwise use current name. If name is "Untitled", ask for a name
- * anyway. Sets the current name if it wasn't already set.
- *
- * The file is deleted if not written properly.
- */
-
- Boolean
- SaveMap (Boolean askForName)
- {
- short f;
- short i;
- OSErr result;
- long pos;
- Boolean ok = false;
- StringHandle hStr;
-
- if (GetOutputFile (askForName, fName, vRefNum, &outFile) == false)
- return (false);
- if (OpenOutputFile (&outFile, mwCreator, mwType, &f) == false)
- return (false);
-
- if (WriteInteger (f, (short) mapVersion)
- && WriteInteger (f, (short) mapList->nLines))
- {
- ok = true;
- for (i = 0; i < mapList->nLines; ++i)
- {
- ok = FileWrite (f, (Ptr) &mapSpec[i], (long) sizeof (MapSpec));
- if (!ok)
- break;
- hStr = mapSpec[i].mark;
- HLock ((Handle) hStr);
- ok = WriteString (f, *hStr);
- HUnlock ((Handle) hStr);
- if (!ok)
- break;
- }
- if (ok)
- {
- ok = FileWrite (f, (Ptr) ¶Style, (long) sizeof (ParaStyle))
- && WriteString (f, paraMark)
- && WriteString (f, pageMark)
- && WriteString (f, periodStr)
- && WriteString (f, quoteStr);
- }
- }
-
- (void) GetFPos (f, &pos);
- (void) SetEOF (f, pos);
- (void) FSClose (f);
- (void) FlushVol (/* f */ nil, outFile.vRefNum);
-
- if (!ok)
- {
- Message1 ("\pCould not write map");
- (void) FSDelete (outFile.fName, outFile.vRefNum);
- return (false);
- }
-
- CopyString (outFile.fName, fName); /* set map name */
- vRefNum = outFile.vRefNum;
- SetMapName (fName);
- return (true);
- }
-